import Tooltip from "../ui/Tooltip";
import Highcharts from "highcharts/highstock";
import Dom from "../utils/Dom";
import Utils from "../utils/index";

class GanttTooltip {
   constructor(Gantt) {
      this.Gantt = Gantt;
      this.tooltip = {
         simple: new Tooltip({
            className: "simple-tooltip"
         }),
         detail: new Tooltip({
            className: "detail-tootlip"
         }),
         heat: new Tooltip({
            className: "heat-tooltip"
         })
      };
      this.castHeatTimer = null;
   }


   simpleFormatter(point) {

      let ss = '<p>' + point.series.options.taskCounter + '</p><p>' + '<span>' +
      Highcharts.dateFormat('%H:%M:%S', point.x) +
      '</span><span style="float: right">' +
      Highcharts.dateFormat('%H:%M:%S', point.x2) +
      '</span></p>';

      let regex = new RegExp('BOF', 'g'); // 使用g表示整个字符串都要匹配
      let result = point.series.name.match(regex);

      let endSs = '<p>' +
      '出钢至开浇时长:'+((point.series.xData[point.series.x2Data.length-1] - point.series.x2Data[result.length - 1])/60000).toFixed(1) + 'min'
      +'</p>';

      if(((point.series.xData[point.series.x2Data.length-1] - point.series.x2Data[result.length - 1])/60000) <= 140 || ((point.series.xData[point.series.x2Data.length-1] - point.series.x2Data[result.length - 1])/60000)>180){
         endSs = '<p>' +
         '<font color = "red">出钢至开浇时长:'+((point.series.xData[point.series.x2Data.length-1] - point.series.x2Data[result.length - 1])/60000).toFixed(1) + 'min'
         +'</font></p>';
      }
      return  ss + endSs;
   }

   detailFormatter(point) {
      let items = [{
               name: "浇次号",
               key: "castNo"
            },
            {
               name: "计划炉次号",
               key: "taskCounter"
            },
            {
               name: "实际炉次号",
               key: "heatId"
            },
            {
               name: "炉次顺序",
               key: "castHtNo"
            },
            {
               name: "出钢标记",
               key: "caDescr",
               flag: "red"
            },
            {
               name: "钢种外部牌号",
               key: "sgSign"
            },
            {
               name: "低N钢种标记",
               key: "lowNFlag"
            },
            {
               name: "S含量要求",
               key: "standards"
            },
            {
               name: "B含量要求",
               key: "standardb"
            },
            {
                name: "P含量要求",
                key: "standardp"
            },
            {
               name: '钢包号',
               key: 'ladleId'
            },
            {
               name: '钢包带渣量',
               key: 'steelPackwt'
            },
            {
               name: '空包重量',
               key: 'nullPackwt'
            },
            {
               name: '钢包状态',
               key: 'ladleState'
            },
            {
               name: "工艺路径",
               key: "name",
               obj: "series",
               flag: "red"
            },
            {
               name: "是否重难点",
               key: "znFlag"
            },
            {
               name: "板坯厚度",
               key: ["minSlabHeight", "maxSlabHeight"]
            },
            {
               name: "板坯宽度",
               key: ["minSlabWidth", "maxSlabWidth"]
            },
            {
               name: "板坯长度",
               key: ["minSlabLength", "maxSlabLength"]
            },
            {
               name: "缓冷、堆冷方式",
               key: "hlDl"
            },
            {
               name: "是否人工转堆冷",
               key: "coldFlag"
            },
            {
               name: "人工转堆冷时长",
               key: "coldDate"
            }
         ],
         innerHTML = "",
         obj = {
            series: point.series,
            point: point
         },
         heat = this.Gantt.cache.getById("heats", point.series.options.heatSid);



      items.forEach(item => {
         let sp = heat['standardp'].substr(0,1);
         heat&&item.flag||(item.key === "znFlag" && heat['znFlag'] === "是")||(sp=='1' && item.key === "standardp") ?
         innerHTML += "<p style='color:red'><b>" + item.name + "：</b>" :
         innerHTML += "<p><b>" + item.name + "：</b>";
         if (item.key) {

            if(item.key === "coldFlag"){
               innerHTML += heat[item.key] == "1" ? "是" : "否";
            }else if(item.key === "coldDate"){
               innerHTML += heat[item.key] ? heat[item.key] : "无";
            }else if(item.key === "standardp"){
                innerHTML += heat[item.key].substr(2);
            }else{
               innerHTML += item.obj ?
               obj[item.obj][item.key] :
               typeof item.key === "string" ?
               heat[item.key] || "" :
               heat[item.key[0]] + " - " + heat[item.key[1]];
            }
         }
         innerHTML += "</p>";
      });

      // 冲突
      let conflictUl = "",
         key = null,
         conflict = null;
      if (point.xConflicted && !Utils.isObjEmpty(point.xConflicted)) {
         for (key in point.xConflicted) {
            conflict = point.xConflicted[key];
            conflictUl +=
               "<li>在时间 " + Utils.shortTimeFormat(conflict.time[0]) + " ~ " + Utils.shortTimeFormat(conflict.time[1]) + " 之间产生了冲突</li>";
         }
      }

      if (point.sConflicted && !Utils.isObjEmpty(point.sConflicted)) {
         for (key in point.sConflicted) {
            conflict = point.sConflicted[key];
            conflictUl +=
               "<li>在设备 " + conflict.facilities[0] + " 和 " + conflict.facilities[1] + " 之间的时间太" + (conflict.type === "min" ? "短" : "长") + "</li>";
         }
      }

      if (point.cConflicted) {
         conflict = point.cConflicted;
         conflictUl +=
            "<li> " +
            (typeof conflict === "string" ?
               conflict :
               "浇次冲突，在 " +
               Utils.shortTimeFormat(conflict[0]) +
               " 和 " +
               Utils.shortTimeFormat(conflict[1]) +
               " 时间之间") +
            "</li>";
      }

      if (point.rConflicted) {
         point.rConflicted.forEach(conflict => {
            conflictUl +=
               "<li>在时间 " +
               Utils.shortTimeFormat(conflict[0]) +
               " 和 " +
               Utils.shortTimeFormat(conflict[1]) +
               " 之间与设备检修时间产生了冲突</li>";
         });
      }

      if (heat.caId && heat.actualCaId && heat.caId !== heat.actualCaId) {
         conflictUl += "<li>钢种发生了变更</li>";
      }

      if (point.caConflict) {
         conflictUl += '<li>钢种 ' + point.caConflict[0] + ' 不同于 ' + point.caConflict[1] + '</li>';
      }

      innerHTML += conflictUl ?
         '<ul class="conflict">' + conflictUl + '</ul>' :
         '';

      return innerHTML;
   }

   position(type, pos, size) {
      pos.x = pos.x - size.w / 2 - 10;
      if (pos.x < 0) {
         pos.x = 0;
      } else if (pos.x + size.w > window.innerWidth) {
         pos.x = window.innerWidth - size.w;
      }

      if (type !== "detail" || pos.y + size.h > window.innerHeight) {
         pos.y = pos.y - size.h - pos.h - 20;
      }
   }

   show(type, point) {
      this.render(type, this[type + "Formatter"](point), point);
   }

   showCastHeat(heatSid, event) {
      let html = "",
         items = [{
               name: "浇次号",
               key: "castNo"
            },
            {
               name: "计划炉次号",
               key: "taskCounter"
            },
            {
               name: "实际炉次号",
               key: "heatId"
            },
            {
               name: "炉次顺序",
               key: "castHtNo"
            },
            {
                name:"轧制计划号",
                key:"precRollPlanNo"
            },
            {
                name: "出钢标记",
                key: "caDescr"
            },
            {
               name: "钢种外部牌号",
               key: "sgSign"
            },
            {
               name: "精炼区分",
               key: "refineDiv"
            },
            {
               name: "板坯厚度",
               key: ["minSlabHeight", "maxSlabHeight"]
            },
            {
               name: "板坯宽度",
               key: ["minSlabWidth", "maxSlabWidth"]
            },
            {
               name: "板坯长度",
               key: ["minSlabLength", "maxSlabLength"]
            },
            {
               name: "缓冷、堆冷方式",
               key: "hlDl"
            },
            {
               name: "是否人工转堆冷",
               key: "coldFlag"
            },
            {
               name: "人工转堆冷时长",
               key: "coldDate"
            }
         ],
         key = null,
         size = null,
         heat = this.Gantt.cache.getById("heats", heatSid),
         castCaConfigs = this.Gantt.cache.getByIndex("castCaConfigs", "caId1", heat.caId);

      items.forEach(item => {
         key = item.key;
         if(key === "coldFlag"){
            let temp = "否";
            if(heat[key] == "1"){
               temp = "是";
            }
            html += "<p><b>" + item.name + "：</b>" + temp +"</p>";
         }else if (key === "coldDate"){
            let temp = "无";
            if(heat[key]){
               temp = heat[key];
            }
            html += "<p><b>" + item.name + "：</b>" + temp +"</p>";
         }else{
            html +=
            "<p><b>" + item.name + "：</b>" +
            (typeof key === "object" ?
               (heat[key[0]] + " ~ " + heat[key[1]]) :
               (heat[key] || "")) +
            "</p>";
         }
      });

      if (castCaConfigs && castCaConfigs.length > 0) {
         html += '<ul>';
         castCaConfigs.forEach(c => {
            html += '<li>钢种 ' + c.caId2 + ' 可以进行连浇</li>';
         });

         html += '</ul>';
      }

      this.tooltip.heat.show(html);
      size = Dom.size(this.tooltip.heat.root);
      Dom.styles(this.tooltip.heat.root, {
         left: event.clientX - size.w / 2 + 'px',
         top: event.clientY - size.h - 20 + 'px'
      });
   }

   render(type, html, point) {
      this.tooltip[type].show(html);
      let chart = point.series.chart,
         position = {
            x: point.plotX + chart.plotLeft + point.shapeArgs.width / 2 + 20,
            y: point.plotY + chart.plotTop + 60 + point.shapeArgs.height,
            h: point.shapeArgs.height
         },
         size = Dom.size(this.tooltip[type].root);

      // 计算位置
      this.position(type, position, size);

      Dom.styles(this.tooltip[type].root, {
         left: position.x + "px",
         top: position.y + "px"
      });
   }

   hide(type) {
      if (type) {
         this.tooltip[type].hide();
      } else {
         for (let key in this.tooltip) {
            this.tooltip[key].hide();
         }
      }
   }
}

export default GanttTooltip;
